<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
				<el-button class="uni-button" type="success" size="mini" @click="navigateTo('./add')">新增</el-button>
				<button class="uni-button" type="default" size="mini" @click="navigateTo('/pages/courses/add')">新增分类</button>
				<button class="uni-button" type="default" size="mini" @click="delTable">批量删除</button>
			</view>
			<view class="uni-group">
				<el-select v-model="query.course_id" placeholder="请选择课程">
					<el-option  label="所有" value="" >
					</el-option>
					<el-option v-for="(item, inx) in queryCourse" :key="inx" :label="item.name" :value="item._id">
					</el-option>
				</el-select>
				<input class="uni-search" type="text" v-model="query.keyword" placeholder="请输入搜索内容" />
				<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
			</view>
		</view>
		<view class="uni-container">
			<unicloud-db
				ref="udb"
				:collection="collectionName"
				:options="options"
				:where="where"
				page-data="replace"
				:orderby="orderby"
				:getcount="true"
				:page-size="options.pageSize"
				:page-current="options.pageCurrent"
				v-slot:default="{ data, pagination, loading, error }"
			>
				<uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe type="selection" @selection-change="selectionChange">
					<uni-tr>
						<!-- <uni-th align="center">ID，系统自动生成</uni-th> -->
						<uni-th align="center">视频名称</uni-th>
						<uni-th align="center">课程名称</uni-th>
						<uni-th align="center">视频封面</uni-th>
						<uni-th align="center">视频地址</uni-th>
						<uni-th align="center">时长，以秒为单位</uni-th>
						<uni-th align="center">排序，正序</uni-th>
						<uni-th width="204" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in data" :key="index">
						<!-- <uni-td align="center">{{ item._id }}</uni-td> -->
						<!-- <uni-td align="center">{{ item._id }}</uni-td> -->
						<uni-td align="center">{{ item.name }}</uni-td>
						<uni-td align="center">{{ courseLi[item.course_id] }}</uni-td>
						<uni-td align="center">
							<el-image style="width: 40px;height: 40px;" :src="item.cover" fit="cover" :preview-src-list="[item.cover]"></el-image>
							<el-link type="primary" style="margin-left:20px" @click="setClipboardData(item.cover)">
								<i class="el-icon-document-copy"></i>
								复制
							</el-link>
						</uni-td>
						<uni-td align="center">
							{{ item.src }}
							<el-link :href="item.src" target="_blank" type="primary">预览</el-link>
							<el-link type="primary" style="margin-left:20px" @click="setClipboardData(item.src)">
								<i class="el-icon-document-copy"></i>
								复制
							</el-link>
						</uni-td>
						<uni-td align="center">{{ item.duration }}</uni-td>
						<uni-td align="center">{{ item.order }}</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button @click="navigateTo('./edit?id=' + item._id)" class="uni-button" size="mini" type="primary">修改</button>
								<button @click="confirmDelete(item._id)" class="uni-button" size="mini" type="warn">删除</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count"
					    @change="onPageChanged" />
				</view>
			</unicloud-db>
		</view>
	</view>
</template>

<script>
const db = uniCloud.database();
// 表查询配置
const dbCollectionName = 'videos,courses';
const dbOrderBy = 'order desc'; // 排序字段
const dbSearchFields = ['name','course_id']; // 支持模糊搜索的字段列表
const fileds = '_id,name,src,duration,course_id{_id,name},order';
// 分页配置
const pageSize = 6;
const pageCurrent = 1;

export default {
	data() {
		return {
			courseLi:[],
			queryCourse:{},
			query: {
				keyword:null,
				course_id:null,
			},
			where: '',
			fileds: fileds,
			orderby: dbOrderBy,
			collectionName: dbCollectionName,
			options: {
				pageSize,
				pageCurrent
			}
		};
	},
	onLoad(e) {
		this.courseList();
	},
	methods: {
		getWhere() {
			let keyword = this.query.keyword;
			if (keyword) {
				keyword=keyword.trim();
			}
			const queryRe = new RegExp(keyword, 'i');
			let s=dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ');
			s+='|| course_id=='+this.query.course_id;
			return s
		},
		courseList(){
			db.collection('courses')
				.field('_id,name,cover')
				.get()
				.then(res => {
					const data = res.result.data;
					if (data) {
						let resLi=[]
						data.forEach(item=>{
							resLi[item._id]=item.name
						})
						this.queryCourse=data
						this.courseLi = resLi;
					}
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		},
		search() {
			const newWhere = this.getWhere();
			const isSameWhere = newWhere === this.where;
			this.where = newWhere;
			if (isSameWhere) {
				// 相同条件时，手动强制刷新
				this.loadData();
			}
		},
		setClipboardData(data) {
			uni.setClipboardData({
				data,
				success(res) {
					uni.showToast({
						icon: 'none',
						title: '复制 成功！'
					});
				},
				fail(res) {
					uni.showModal({
						content: '复制  失败！',
						showCancel: false
					});
				}
			});
		},
		loadData(clear = true) {
			this.$refs.udb.loadData({
				clear
			});
		},
		onPageChanged(e) {
			this.$refs.udb.loadData({
				current: e.current
			});
		},
		navigateTo(url) {
			uni.navigateTo({
				url,
				events: {
					refreshData: () => {
						this.loadData();
					}
				}
			});
		},
		// 多选处理
		selectedItems() {
			var dataList = this.$refs.udb.dataList;
			return this.selectedIndexs.map(i => dataList[i]._id);
		},
		//批量删除
		delTable() {
			this.$refs.udb.remove(this.selectedItems());
		},
		// 多选
		selectionChange(e) {
			this.selectedIndexs = e.detail.index;
		},
		confirmDelete(id) {
			this.$refs.udb.remove(id);
		}
	}
};
</script>
<style lang="scss">
.suoluetu {
	width: 100rpx;
	height: 100rpx;
}
</style>
